package interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

import utils.StringUtil;

public class TrimInterceptor implements Interceptor {
	private static final long serialVersionUID = -2578561479301489061L;

	public void destroy() {
	}

	public void init() {
		System.out.println("特殊字符拦截器初始化");
	}
	
	public String intercept(ActionInvocation invocation) throws Exception {

		Map<String, Object> map = invocation.getInvocationContext().getParameters();
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			map.put(entry.getKey(), transfer((String[]) entry.getValue()));
		}
		return invocation.invoke();
	}

	/**
	 * @Description: 遍历参数数组里面的数据，取出空格
	 * @param params
	 * @return
	 */
	private String[] transfer(String[] params) {
		for (int i = 0; i < params.length; i++) {
			if (StringUtil.isEmpty(params[i])) {
				continue;
			}
			params[i] = params[i].trim();
		}
		return params;
	}

}
